package com.infinite.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {

    @Autowired
    JdbcTemplate jdbcTemplate;


    @RequestMapping("mysql/list")
    public List<Map<String,Object>> list(){
        String sql = "select * from user";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        return mapList;
    }

    @RequestMapping("mysql/insert")
    public String insert(){
        String sql = "insert into user values(null,'小明','123456')";
        jdbcTemplate.update(sql);
        return "insert_success";
    }
    @RequestMapping("mysql/update/{id}")
    public String update(@PathVariable("id") Integer id){
        String sql = "update user set username=?,password=? where id="+id;
        Object[] objects = {"小王","1234"};
        jdbcTemplate.update(sql,objects);
        return "update_success";
    }
    @RequestMapping("mysql/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        String sql = "delete from user where id = ?";
        jdbcTemplate.update(sql,id);
        return "delete_success";
    }

}
